//
//  GGChatCell.m
//  Gunterapple
//
//  Created by 热带雨林 on 2020/4/2.
//  Copyright © 2020 热带雨林. All rights reserved.
//

#import "GGChatCell.h"
#import "GGAddtion.h"
#import "ZFBChatModel.h"
@interface GGChatCell ()
@property (nonatomic, weak) UILabel *timelab;
@property (nonatomic, weak) UIImageView *icon;
@property (nonatomic, weak) UIImageView *backImg;
@property (nonatomic, weak) UILabel *messageLab;
@end
@implementation GGChatCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI{
    UILabel *timelab = [[UILabel alloc]init];
    [self.contentView addSubview:timelab];
    self.timelab = timelab;
    self.timelab.font = [UIFont systemFontOfSize:13];
    [self.timelab sizeToFit];
    
    UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectZero];
    self.icon = icon;
    self.icon.layer.cornerRadius = 10;
    self.icon.clipsToBounds = YES;
    [self.contentView addSubview:self.icon];
    
    UIImageView *backImg = [[UIImageView alloc]initWithFrame:CGRectZero];
    self.backImg = backImg;
    [self.contentView addSubview:self.backImg];

    UILabel *messageLab = [[UILabel alloc]init];
    self.messageLab = messageLab;
    self.messageLab.numberOfLines = 0;
    self.messageLab.font = [UIFont systemFontOfSize:15];
    [self.messageLab sizeToFit];
    [self.contentView addSubview:self.messageLab];
    
    [self.timelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(8);
        make.centerX.equalTo(self.contentView);
    }];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.timelab.mas_bottom).offset(8);
        make.right.equalTo(self.contentView).offset(-8);
        make.size.mas_equalTo(CGSizeMake(45, 45));
    }];
    [self.backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icon);
        make.right.equalTo(self.icon.mas_left).offset(-8);
        make.width.lessThanOrEqualTo(@200);
    }];
    [self.messageLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.timelab.mas_bottom).offset(8);
//        make.right.equalTo(self.backImg.mas_right).offset(-16);
//        make.left.equalTo(self.backImg.mas_left).offset(8);
//        make.bottom.equalTo(self.backImg.mas_bottom).offset(-8);
        //在设置约束中是可以相对于两个不同的控件设置底部的约束
        make.edges.equalTo(self.backImg).mas_offset(UIEdgeInsetsMake(8, 8, 8, 16));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
    }];
//    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.right.left.equalTo(self);
//        make.bottom.mas_greaterThanOrEqualTo(self.backImg.mas_bottom).offset(8);
//    }];
}
-(void)setModel:(ZFBChatModel *)model{
    _model = model;
    self.timelab.text = model.time;
    if(model.type == kMessageTypeMe){
        self.icon.image = [UIImage imageNamed:@"user01_icon"];
    }else{
        self.icon.image = [UIImage imageNamed:@"user02_icon"];
    }
    //聊天的背景图片
    UIImage *image = [UIImage imageNamed:@"Dialog_green.right"];
    /*
     LeftCapWidth:第一个是左边不拉伸区域的宽度
     topCapHeight:第二个参数是上面不拉伸的高度
     */
    [image stretchableImageWithLeftCapWidth:image.size.width / 2 topCapHeight:image.size.height * 0.7];
    self.backImg.image = image;
    self.messageLab.text = model.text;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
